package com.wzy.scns.controller;

import com.wzy.scns.entity.Student;
import com.wzy.scns.entity.Teacher;
import com.wzy.scns.repositery.SelectCourseRepository;
import com.wzy.scns.repositery.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.*;

@Validated
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    StudentRepository studentRepository;
    @Autowired
    SelectCourseRepository selectCourseRepository;
    @Validated
    @PostMapping("/add")
    public Map add(@NotNull(message="姓名不能为空") String name,
                   @NotNull(message="学号不能为空") String code,
                   String phone,
                   @NotNull(message="密码不能为空") String password,
                   @NotNull(message="系部不能为空") String department){
        List<String> msgs =new ArrayList<>();
        if (studentRepository.getStudentByCode(code)!=null)
            msgs.add("学号已存在");
        if (studentRepository.findByName(name)!=null)
            msgs.add("姓名已存在");
        if(msgs.size()>0)
            return makeErrorMap(msgs);
        Student student = new Student();
        student.setCode(code);
        student.setName(name);
        student.setPhone(phone);
        student.setPassword(password);
        student.setDepartment(department);
        List<Student> students =new ArrayList<>();
        students.add(studentRepository.save(student));
        if(students.size()>0)
            return makeMap(students,1);
        else
            return makeMap( students,0);

    }
    @PostMapping("/delete")
    public Map delete(@NotNull String code){
        Student student = studentRepository.findByCode(code);
        if(student==null)
            return errorMap("该学号不存在");
        studentRepository.deleteStudentByCode(code);
        int a = selectCourseRepository.deleteAllByStudentCode(student.getCode());
        return postMap("注销成功",a+1);
    }
    @PostMapping("/updataById")
    public Map updataById(
            @NotNull(message = "id不能为空") Integer id,
            @NotNull(message = "姓名不能为空") String name,
            @NotNull(message = "学号不能为空") String code,
            String phone,
            @NotNull(message = "密码不能为空") String password,
            @NotNull(message = "系部不能为空") String department) {
        Student studentById = studentRepository.findById(id).get();
        List<String> msgs = new ArrayList<>();
        if(studentById==null)
            return errorMap("未找到该id所匹配的学生");
        if (studentRepository.findByNameAndIdIsNot(name,studentById.getId())!=null)
            msgs.add("姓名已存在");
        if (msgs.size() > 0)
            return makeErrorMap(msgs);
        studentById.setName(name);
        studentById.setCode(code);
        studentById.setPhone(phone);
        studentById.setPassword(password);
        studentById.setDepartment(department);
        studentRepository.save(studentById);
        Map map = new HashMap();
        map.put("code",1);
        map.put("student",studentById);
        return map;
    }


    //查询
    //姓名密码登录
    @GetMapping("/loginName")
    public Map loginName(@NotNull String name,@NotNull String password){
        Map map = new HashMap();
        Student student = studentRepository.getStudentByNameAndPassword(name,password);
        if (student != null)
            return getMap("登录成功",student);
        student = studentRepository.findByName(name);
        if(student==null)
            return errorMap("姓名不存在");
        return errorMap("密码错误");
    }
    @GetMapping("/login")
    public Map login(@NotNull String code, @NotNull String password) {
        Student student = studentRepository.getStudentByCodeAndPassword(code,password);
        if (student != null)
            return getMap("登录成功",student);
        student = studentRepository.findByCode(code);
        if(student==null)
            return errorMap("学号不存在");
        return errorMap("密码错误");
    }


    @GetMapping("/getStudentById")
    public Map getStudentInfo(Integer id){
        Student student = studentRepository.getStudentById(id);
        if(student!= null)
            return userMsgMap( student,200);
        else
            return errorMap("该id不存在");
    }
    @GetMapping("/getStudentByCode")
    public Map getStudentById(String code){
        Student student = studentRepository.getStudentByCode(code);
        if(student!= null)
            return getMap("查询成功",student);
        else
            return errorMap("该学号不存在");
    }




    public Map userMsgMap(Student student,int code){
        Map map = new HashMap();
        map.put("student",student);
        map.put("code",code);
        return map;
    }
    public Map makeMap(List data,int code){
        Map map = new HashMap();
        map.put("total",data.size());
        map.put("data",data);
        map.put("code",code);
        return map;
    }
    public Map makeErrorMap(List<String> msg) {
        Map map = new HashMap();
        map.put("msg", msg);
        map.put("code", 0);
        return map;
    }


    public Map getMap(String msg, Object data) {
        Map map = new HashMap();
        map.put("data", data);
        map.put("msg", msg);
        map.put("code", 1);
        return map;
    }

    public Map postMap(String msg, Integer totalSelf) {
        Map map = new HashMap();
        map.put("code", 1);
        map.put("msg", msg);
        map.put("total", totalSelf);
        return map;
    }

    public Map errorMap(String msg) {
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", msg);
        return map;
    }
//    public List<String> Msg(Student student){
//        List<String> msgs = new ArrayList<>();
//        if (baseRepository.getStudentByCode(student.getCode())!=null)
//            msgs.add("学号已存在");
//        if (baseRepository.getStudentByCode(student.getName())!=null)
//            msgs.add("姓名已存在");
//    }
}
